/**   
 * @Title: ChooseServiceImplementation.java 
 * @Package cn.edu.cdtu.healthserver.service.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.edu.cdtu.healthserver.dao.ChooseDao;
import cn.edu.cdtu.healthserver.dao.impl.ChooseDaoImplementation;
import cn.edu.cdtu.healthserver.domain.Choose;
import cn.edu.cdtu.healthserver.domain.Question;
import cn.edu.cdtu.healthserver.service.ChooseService;

/** 
 * @ClassName: ChooseServiceImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class ChooseServiceImplementation implements ChooseService {
	
	private ChooseDao chooseDao = new ChooseDaoImplementation();

	/* (non-Javadoc)
	 * <p>Title: getChooses</p> 
	 * <p>Description: </p> 
	 * @param question
	 * @param listAvailable
	 * @return 
	 * @see cn.edu.scu.healthserver.service.ChooseService#getChooses(cn.edu.scu.healthserver.domain.Question, boolean)
	 */
	@Override
	public List<Choose> getChooses(Question question, boolean listAvailable) {
		// TODO Auto-generated method stub
		List<Choose> chooses = chooseDao.query(question);
		if(listAvailable){
			return chooses;
		}
		List<Choose> removeList = new ArrayList<Choose>();
		for (Choose choose : chooses) {
			if(0==choose.getAvailable()){
				removeList.add(choose);
			}
		}
		for (Choose choose : removeList) {
			chooses.remove(choose);
		}
		return chooses;
	}

	/* (non-Javadoc)
	 * <p>Title: addChoose</p> 
	 * <p>Description: </p> 
	 * @param question
	 * @param choose
	 * @return 
	 * @see cn.edu.scu.healthserver.service.ChooseService#addChoose(cn.edu.scu.healthserver.domain.Question, cn.edu.scu.healthserver.domain.Choose)
	 */
	@Override
	public String addChoose(Question question, Choose choose) {
		// TODO Auto-generated method stub
		String resultString = null;
		choose.setQuestion_id(question.getId());
		if(chooseDao.add(choose)){
			resultString = "添加选项成功！";
		}else {
			resultString = "添加选项失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: updateChoose</p> 
	 * <p>Description: </p> 
	 * @param choose
	 * @return 
	 * @see cn.edu.scu.healthserver.service.ChooseService#updateChoose(cn.edu.scu.healthserver.domain.Choose)
	 */
	@Override
	public String updateChoose(Choose choose) {
		// TODO Auto-generated method stub
		String resultString = null;
		if(chooseDao.update(choose)){
			resultString = "更新选项成功！";
		}else {
			resultString = "更新选项失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: setAvailable</p> 
	 * <p>Description: </p> 
	 * @param choose
	 * @param available
	 * @return 
	 * @see cn.edu.scu.healthserver.service.ChooseService#setAvailable(cn.edu.scu.healthserver.domain.Choose, boolean)
	 */
	@Override
	public String setAvailable(Choose choose, boolean available) {
		// TODO Auto-generated method stub
		String resultString = null;
		if (available) {
			choose.setAvailable(1);
			if(chooseDao.update(choose)){
				resultString = "设置选项有效成功！";
			}else {
				resultString = "设置选项有效失败！";
			}
		} else {
			choose.setAvailable(0);
			if(chooseDao.update(choose)){
				resultString = "设置选项失效成功！";
			}else {
				resultString = "设置选项失效失败！";
			}
		}
		return resultString;
	}

}
